def find_insert_position(nums, target):
    """
    在已排序的数组中找到插入位置
    :param nums: 已排序的数组，例如 [1, 3, 5, 6]
    :param target: 待插入的元素，例如 5
    :return: 插入位置的索引
    """
    left, right = 0, len(nums) - 1

    while left <= right:
        mid = (left + right) // 2
        if nums[mid] == target:
            return mid
        elif nums[mid] < target:
            left = mid + 1
        else:
            right = mid - 1

    return left


if __name__ == '__main__':
    print(find_insert_position([1, 3, 5, 6], 5))
    print(find_insert_position([1, 3, 5, 6], 2))
    print(find_insert_position([1, 3, 5, 6], 7))
    print(find_insert_position([1, 3, 5, 6], 0))
